package com.wanxi.service.impl;

import com.wanxi.feign.ProductFeignClient;
import com.wanxi.mapper.ProductMapper;
import com.wanxi.model.ClassifyQuery;
import com.wanxi.model.ProductInstanceQuery;
import com.wanxi.model.ProductQuery;
import com.wanxi.result.CommonResult;
import com.wanxi.dto.ClassifyDTO;
import com.wanxi.dto.ProductInstanceDTO;
import com.wanxi.dto.ProductDTO;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(description = "产品服务")
@RestController
@RequestMapping("/product")
public class ProductServiceImpl implements ProductFeignClient {


    @Autowired
    ProductMapper productMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final int DOCTOR_NUM = 2;

    //服务包增删查改
    //添加服务包
    @PostMapping("/addProduct")
    @Override
    public CommonResult addProduct(ProductDTO product) {
        logger.info("入参：" + product);
        int i = productMapper.addProduct(product);
        if (i > 0) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    @PostMapping("/insertDoctorToProduct")
    @Override
    public CommonResult insertDoctorToProduct(ArrayList<Long> doctorIds, Long productId) {
        logger.info("入参：" + doctorIds + productId);
        int listSize = doctorIds.size(); //集合长度
        int toIndex = DOCTOR_NUM;  //批量插入索引范围
        int num = 0;  //批量成功插入次数
        for (int i = 0; i < doctorIds.size(); i = i + DOCTOR_NUM) {
            if (i + DOCTOR_NUM > listSize) {
                toIndex = listSize - i;
            }
            List<Long> doctorIdList = doctorIds.subList(i, i + toIndex);
            num += productMapper.insertDoctorToProduct(doctorIdList, productId);
            logger.info("出参：" + num);
        }
        if (num == listSize) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    //修改服务包
    @PutMapping("/updateProduct")
    @Override
    public CommonResult updateProduct(long id, ProductDTO product) {
        logger.info("入参：" + id + "," + product);
        int i = productMapper.updateProduct(id, product);
        if (i > 0) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    //    修改服务包状态
    @PutMapping("/updateProductState")
    @Override
    public CommonResult updateProductState(Long id, Integer state) {
        logger.info("入参：" + id + "," + state);
        int i = productMapper.updateProductState(id, state);
        if (i > 0) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    //查找服务包
    @GetMapping("/findProduct")
    @Override
    public CommonResult findProduct(ProductQuery productQuery) {
        logger.info("入参：" + productQuery);
        return CommonResult.success(productMapper.findProduct(productQuery));
    }


    @GetMapping("/findProductDetail")
    @Override
    public CommonResult findProductDetail(Long productId) {
        logger.info("入参：" + productId);
        return CommonResult.success(productMapper.findProductDetail(productId));
    }

    //    后台查找服务包
    @GetMapping("/findProductBack")
    @Override
    public CommonResult findProductBack(ProductQuery productQuery) {
        logger.info("入参：" + productQuery);
        return CommonResult.success(productMapper.findProductBack(productQuery));
    }

    //    服务包实体
//    添加服务包实体
    @PostMapping("/addProductInstance")
    @Override
    public CommonResult addProductInstance(ProductInstanceDTO productInstance) {
        logger.info("入参：" + productInstance);
        int i = productMapper.addProductInstance(productInstance);
        if (i > 0) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }


    //    查找服务包实体
    @GetMapping("/findProductInstance")
    @Override
    public CommonResult findProductInstance(ProductInstanceQuery productInstanceQuery) {
        logger.info("入参：" + productInstanceQuery);
        return CommonResult.success(productMapper.findProductInstance(productInstanceQuery));
    }

    //    后台查找服务包实体
    @GetMapping("/findProductInstanceBack")
    @Override
    public CommonResult findProductInstanceBack(ProductInstanceQuery productInstanceQuery) {
        logger.info("入参：" + productInstanceQuery);
        return CommonResult.success(productMapper.findProductInstanceBack(productInstanceQuery));
    }

    //分类
//    查找分类
    @GetMapping("/findClassify")
    @Override
    public CommonResult findClassify(ClassifyQuery classifyQuery) {
        logger.info("入参：" + classifyQuery);
        return CommonResult.success(productMapper.findClassify(classifyQuery));
    }

    //    添加分类
    @PostMapping("/addClassify")
    @Override
    public CommonResult addClassify(ClassifyDTO classify) {
        logger.info("入参：" + classify);
        int i = productMapper.addClassify(classify);
        if (i > 0) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    //    修改分类
    @PutMapping("/updateClassify")
    @Override
    public CommonResult updateClassify(Long id, ClassifyDTO classify) {
        logger.info("入参：" + classify);
        int i = productMapper.updateClassify(id, classify);
        if (i > 0) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    //    修改分类状态
    @PutMapping("/updateClassifyState")
    @Override
    public CommonResult updateClassifyState(Long id, Integer state) {
        logger.info("入参：" + id + "," + "state");
        int i = productMapper.updateClassifyState(id, state);
        if (i > 0) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    //    后台查找分类
    @GetMapping("/findClassifyBack")
    @Override
    public CommonResult findClassifyBack(ClassifyQuery classifyQuery) {
        logger.info("入参：" + classifyQuery);
        return CommonResult.success(productMapper.findClassifyBack(classifyQuery));
    }
}
